package com.neusoft.ysu.populationmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.ysu.populationmanagement.constant.TabelDic;
import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.Operator;
import com.neusoft.ysu.populationmanagement.entity.PoliceStation;
import com.neusoft.ysu.populationmanagement.mapper.OperatorMapper;
import com.neusoft.ysu.populationmanagement.mapper.PoliceStationMapper;
import com.neusoft.ysu.populationmanagement.service.IOperatorService;
import com.neusoft.ysu.populationmanagement.service.IPoliceStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lhn
 * @since 2025-06-13
 */
@Service
public class PoliceStationServiceImpl implements IPoliceStationService {
    @Autowired
    private PoliceStationMapper policeStationMapper;

    @Autowired
    private IOperatorService operatorService;

    @Override
    public CommonResult selectPoliceStationInfoByPid(Long pid) {
        LambdaQueryWrapper<PoliceStation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PoliceStation::getPid, pid).eq(PoliceStation::getIsDelete, TabelDic.IS_DELETE_NO);
        List<PoliceStation> policeStations = policeStationMapper.selectList(lambdaQueryWrapper);
        if (policeStations.size() <= 0) {
            return new CommonResult<>(0, "未检索到派出所信息", null);
        }
        return new CommonResult<>(1, "派出所信息", policeStations.get(0));
    }

    /**
     * @param policeStation
     * @return CommonResult
     * @description 添加派出所信息
     * @author zyk
     * @date 2025-06-16
     */

    @Override
    public CommonResult registeredPoliceStation(PoliceStation policeStation) {
        //判断一下编号重没重
        CommonResult selectInfo = selectPoliceStationInfoByPNum(policeStation.getPNum());
        if (selectInfo.getCode() != 0) {
            return new CommonResult<>(501, "派出所编号重复", null);
        }
        int rows = policeStationMapper.insert(policeStation);
        if (rows <= 0) {
            return new CommonResult<>(501, "派出所编号重复", null);
        }
        return new CommonResult<>(1, "派出所注册成功", null);
    }

    /**
     * @param pNum
     * @return CommonResult
     * @description 根据编号查派出所信息
     * @author zyk
     * @date 2025-06-16
     */
    @Override
    public CommonResult selectPoliceStationInfoByPNum(String pNum) {
        LambdaQueryWrapper<PoliceStation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PoliceStation::getPNum, pNum).eq(PoliceStation::getIsDelete, TabelDic.IS_DELETE_NO);
        List<PoliceStation> policeStations = policeStationMapper.selectList(lambdaQueryWrapper);
        if (policeStations.size() <= 0) {
            return new CommonResult<>(0, "为检索到派出所信息", null);
        }
        return new CommonResult<>(1, "派出所信息", policeStations.get(0));

    }

    /***
     * @param policeStation
     * @return CommonResult:1,502
     * @description 删除警局和相关的公安干警
     * @author zyk
     * @date 2025-06-16
     */

    @Override
    public CommonResult deletePoliceStation(PoliceStation policeStation) {
        //删除警局信息
        policeStation.setIsDelete(TabelDic.IS_DELETE_YES);
        int rows = policeStationMapper.updateById(policeStation);
        if (rows <= 0) {
            return new CommonResult<>(502, "派出所删除失败", null);
        }
        //删除警员信息
        CommonResult deleteOperator = operatorService.deleteByPid(policeStation.getPid());
        if (deleteOperator.getCode() != 1) {
            deleteOperator.setCode(502);
            return deleteOperator;
        }
        return new CommonResult<>(1, "派出所及所属公安干警已删除", null);

    }

    @Override
    public CommonResult selectAllPolice() {
        List<PoliceStation> psList = new ArrayList<>();
        Map<Long, List<Operator>> opMap = new HashMap<>();
        List<Operator> opList = operatorService.selectAll();
        //获取到所有警员信息并映射成Map格式
        for (Operator op : opList) {
            if (opMap.containsKey(op.getPid())) {
                opMap.get(op.getPid()).add(op);
            } else {
                List<Operator> temp =new ArrayList<>();
                temp.add(op);
                opMap.put(op.getPid(),temp);
            }
        }
        for (Long k:opMap.keySet()) {
            PoliceStation ptemp=policeStationMapper.selectById(opMap.get(k).get(0).getPid());
            ptemp.setOperators(opMap.get(k));
            psList.add(ptemp);
        }

        return new CommonResult<>(1,"警局及警员信息检索成功",psList);

    }
}
